function PostListAssistant(argFromPusher) {
}

PostListAssistant.prototype = {
	setup: function() {
		Ares.setupSceneAssistant(this);
		
		postListXML = null;
		activePostID = null;
		activePostXML = null;
		
		this.controller.setupWidget(Mojo.Menu.viewMenu,
			{
				spacerHeight: 45//,
				//menuClass:'no-fade'
			},
			{
				visible: true,
				items: [
					{items: [
						{label: activeBlogTitle, width: Mojo.Environment.DeviceInfo.screenWidth}
					]}
				]
			}
		);
		
		this.controller.setupWidget(Mojo.Menu.commandMenu, undefined,
		{
			visible: true,
			items:
			[
			{label: "New Post", icon: "new", command: "new_post"}
			]
		});
		
		this.postListSpinner = this.controller.get("postListSpinner");
		this.controller.setupWidget("postListSpinner", {'spinnerSize':Mojo.Widget.spinnerLarge}, this.spinnerModel = {'spinning': true});
		this.postListSpinnerScrim = Mojo.View.createScrim(this.controller.document, {scrimClass: 'palm-scrim'});
		this.postListSpinnerScrim.hide();
		this.controller.get("postListScrimContainer").appendChild(this.postListSpinnerScrim).appendChild(this.postListSpinner);
		
		this.tappedPostListEventListener = this.tappedPostList.bindAsEventListener(this);
		this.deletePostListEventListener = this.deletePostList.bindAsEventListener(this);
		this.controller.listen('postList', Mojo.Event.listTap, this.tappedPostListEventListener);
		this.controller.listen('postList', Mojo.Event.listDelete, this.deletePostListEventListener);
	},
	
	activate: function()
	{
		this.postListSpinnerScrim.show();
		var postListRequest = new Ajax.Request("http://www.blogger.com/feeds/" + activeBlogID + "/posts/default",
		{
			method: 'get',
			requestHeaders: {Authorization: 'GoogleLogin auth=' + authCode},
			onSuccess: this.postListRequestSuccess.bind(this),
			onFailure: this.postListRequestFailure.bind(this)
		});
	},
	
	cleanup: function() {
		this.controller.stopListening('postList', Mojo.Event.listTap, this.tappedPostListEventListener);
		this.controller.stopListening('postList', Mojo.Event.listDelete, this.deletePostListEventListener);
		Ares.cleanupSceneAssistant(this);
	},
	
	handleCommand: function(event)
	{
		if (event.type == Mojo.Event.command)
		{
			switch (event.command)
			{
				case "new_post":
					this.addToPostList(event);
					break;
				default:
					break;
			}
		}
	},
	
	postListRequestSuccess: function(postListResponse)
	{
		postListXML = postListResponse.responseXML;
		var postEntryList = postListXML.getElementsByTagName("entry");
		this.postListModel.items = [];
		//this.controller.modelChanged(this.postListModel);
		for (var postEntryIndex = 0; postEntryIndex < postEntryList.length; postEntryIndex++)
		{
			//Mojo.Log.info(postEntryList[postEntryIndex].getElementsByTagName("title")[0].childNodes[0].nodeValue);
			//Mojo.Log.info(postEntryList[postEntryIndex].getElementsByTagName("draft").length);
			this.postListModel.items.push({
				label: postEntryList[postEntryIndex].getElementsByTagName("title")[0].childNodes.length > 0 ? postEntryList[postEntryIndex].getElementsByTagName("title")[0].childNodes[0].nodeValue : "(Untitled Post)",
				id: postEntryList[postEntryIndex].getElementsByTagName("id")[0].childNodes[0].nodeValue,
				draft: postEntryList[postEntryIndex].getElementsByTagName("draft").length > 0 && postEntryList[postEntryIndex].getElementsByTagName("draft")[0].childNodes[0].nodeValue == 'yes' ? "draft" : "",
				index: postEntryIndex
				});
		}
		this.postListModel.items.push({});
		this.controller.modelChanged(this.postListModel);
		this.postListSpinnerScrim.hide();
	},
	
	postListRequestFailure: function(postListResponse)
	{
		Mojo.Log.info("Failed to get the post list:  " + postListResponse.getAllHeaders());
		Mojo.Log.info(postListResponse.responseText);
		Mojo.Controller.errorDialog("Failed to load the post list.");
		this.postListSpinnerScrim.hide();
	},
	
	tappedPostList: function(event)
	{
		activePostID = event.item.id.split('.').pop().split('-').pop();
		activePostXML = postListXML.getElementsByTagName("entry")[event.item.index];
		Mojo.Log.info(event.item.id);
		Mojo.Log.info(activePostID);
		this.controller.stageController.pushScene({name: 'post-editor'/*, disableSceneScroller: true*/});
	},
	
	deletePostList: function(event)
	{
		var deletePostID = event.item.id.split('.').pop().split('-').pop();
		var deleteRequest = new CustomRequest("http://www.blogger.com/feeds/" + activeBlogID + "/posts/default/" + deletePostID,
		{
			method: 'DELETE',
			requestHeaders:
			{
				Authorization: 'GoogleLogin auth=' + authCode,
				//"X-HTTP-Method-Override": "DELETE",
				"If-Match": "*"
			},
			onSuccess: this.deletePostRequestSuccess.bind(this),
			onFailure: this.deletePostRequestFailure.bind(this)
		});
	},
	
	addToPostList: function(event)
	{
		activePostID = null;
		activePostXML = null;
		this.controller.stageController.pushScene({name: 'post-editor'/*, disableSceneScroller: true*/});
	},
	
	deletePostRequestSuccess: function(deleteRequestResponse)
	{
		Mojo.Log.info(deleteRequestResponse.responseText);
		Mojo.Log.info(deleteRequestResponse.getAllHeaders());
		Mojo.Log.info("post delete succeeded");
	},
	
	deletePostRequestFailure: function(deleteRequestResponse)
	{
		//display error dialog
		Mojo.Log.info(deleteRequestResponse.responseText);
		Mojo.Log.info("post delete failed");
	}
};